package cn.yinyu.queue.module.ecg.controller.admin.devmanage;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.yinyu.queue.framework.common.pojo.PageParam;
import cn.yinyu.queue.framework.common.pojo.PageResult;
import cn.yinyu.queue.framework.common.pojo.CommonResult;
import cn.yinyu.queue.framework.common.util.object.BeanUtils;
import static cn.yinyu.queue.framework.common.pojo.CommonResult.success;

import cn.yinyu.queue.framework.excel.core.util.ExcelUtils;

import cn.yinyu.queue.framework.apilog.core.annotation.ApiAccessLog;
import static cn.yinyu.queue.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.yinyu.queue.module.ecg.controller.admin.devmanage.vo.*;
import cn.yinyu.queue.module.ecg.dal.dataobject.devmanage.DevModelDO;
import cn.yinyu.queue.module.ecg.service.devmanage.DevModelService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 设备型号")
@RestController
@RequestMapping("/ecg/dev-model")
@Validated
public class DevModelController {

    @Resource
    private DevModelService devModelService;

    @PostMapping("/create")
    @Operation(summary = "创建设备型号")
    @PreAuthorize("@ss.hasPermission('ecg:dev-model:create')")
    public CommonResult<Integer> createDevModel(@Valid @RequestBody DevModelSaveReqVO createReqVO) {
        return success(devModelService.createDevModel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备型号")
    @PreAuthorize("@ss.hasPermission('ecg:dev-model:update')")
    public CommonResult<Boolean> updateDevModel(@Valid @RequestBody DevModelSaveReqVO updateReqVO) {
        devModelService.updateDevModel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备型号")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ecg:dev-model:delete')")
    public CommonResult<Boolean> deleteDevModel(@RequestParam("id") Integer id) {
        devModelService.deleteDevModel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备型号")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ecg:dev-model:query')")
    public CommonResult<DevModelRespVO> getDevModel(@RequestParam("id") Integer id) {
        DevModelDO devModel = devModelService.getDevModel(id);
        return success(BeanUtils.toBean(devModel, DevModelRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备型号分页")
    @PreAuthorize("@ss.hasPermission('ecg:dev-model:query')")
    public CommonResult<PageResult<DevModelRespVO>> getDevModelPage(@Valid DevModelPageReqVO pageReqVO) {
        PageResult<DevModelDO> pageResult = devModelService.getDevModelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DevModelRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备型号 Excel")
    @PreAuthorize("@ss.hasPermission('ecg:dev-model:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDevModelExcel(@Valid DevModelPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DevModelDO> list = devModelService.getDevModelPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备型号.xls", "数据", DevModelRespVO.class,
                        BeanUtils.toBean(list, DevModelRespVO.class));
    }

    @GetMapping("/brand-option")
    @Operation(summary = "获得品牌选项")
    @PreAuthorize("@ss.hasPermission('ecg:dev-model:query')")
    public CommonResult<List<OptionVO>> getBrandOption(String category) {
        List<OptionVO> optionVOList = devModelService.getBrandOptions( category );
        return success( optionVOList );
    }

    @GetMapping("/model-option")
    @Operation(summary = "获得模型选项")
    @PreAuthorize("@ss.hasPermission('ecg:dev-model:query')")
    public CommonResult<List<OptionVO>> getModelOption(String category, String brand) {
        List<OptionVO> optionVOList = devModelService.getBrandModelOptions( category, brand );
        return success( optionVOList );
    }

}